﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonTools;
using MotionIoLib;
using Communicate;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using UserCtrl;
using BaseDll;
using UserData;
using HalconDotNet;
using System.IO;
using OtherDevice;
using System.Threading.Tasks;
using PlugIn;
using Sunny.UI;
using VisionBaseLib;
using HY_UPHMgr;
using DBMgrLib;

using System.Drawing;
using StationDemo.工站;
using StationDemo.设备信息.helper;

namespace StationDemo
{
    public class UserConfig
    {

        public static string str_SN = "";
        public static bool b_isScan = false;

        public static bool CleanProduct { get; set; } = false;//是否清料

        public static void CloseHardWork()
        {
            ResetIO2();
            AlarmMgr.GetIntance().StopAlarmBeet();
            TcpMgr.GetInstance().CloseAllEth();
            MotionMgr.GetInstace().Close();
            IOMgr.GetInstace().Close();
        }

        public static void ResetIO()
        {
            //停止时
            IO_OUT.蜂鸣.Set(false);
        }

        public static void ResetIO2()
        {
            //关闭时
            ResetIO();
            IO_OUT.蜂鸣.Set(false);
            IO_OUT.红灯.Set(false);
            IO_OUT.绿灯.Set(false);
            IO_OUT.黄灯.Set(false);
        }

        public static void AddStation()
        {
            Form_Manual三角测试 form_Manual三角测试A = new Form_Manual三角测试(Station.三角测试1.ToString()) { Dock = DockStyle.Fill }; form_Manual三角测试A.Show();
            Station_三角测量 station_三角A = new Station_三角测量(StationMgr.GetInstance().GetStation(Station.三角测试1.ToString()), new Param_CamTriTest(), new Point_CamTriTest(), new Config_CamTriTest());
            StationMgr.GetInstance().AddStation(new StationForm(station_三角A, station_三角A.station_Param, station_三角A.station_Point, station_三角A.station_Config, form_Manual三角测试A), Station.三角测试1.ToString(), station_三角A);

            Form_Manual三角测试 form_Manual三角测试B = new Form_Manual三角测试(Station.三角测试2.ToString()) { Dock = DockStyle.Fill }; form_Manual三角测试B.Show();
            Station_三角测量 station_三角B = new Station_三角测量(StationMgr.GetInstance().GetStation(Station.三角测试2.ToString()), new Param_CamTriTest(), new Point_CamTriTest(), new Config_CamTriTest());
            StationMgr.GetInstance().AddStation(new StationForm(station_三角B, station_三角B.station_Param, station_三角B.station_Point, station_三角B.station_Config, form_Manual三角测试B), Station.三角测试2.ToString(), station_三角B);

            Form_Manual三角测试 form_Manual三角测试C = new Form_Manual三角测试(Station.三角测试3.ToString()) { Dock = DockStyle.Fill }; form_Manual三角测试C.Show();
            Station_三角测量 station_三角C = new Station_三角测量(StationMgr.GetInstance().GetStation(Station.三角测试3.ToString()), new Param_CamTriTest(), new Point_CamTriTest(), new Config_CamTriTest());
            StationMgr.GetInstance().AddStation(new StationForm(station_三角C, station_三角C.station_Param, station_三角C.station_Point, station_三角C.station_Config, form_Manual三角测试C), Station.三角测试3.ToString(), station_三角C);

            Form_Manual三角测试 form_Manual三角测试D = new Form_Manual三角测试(Station.三角测试4.ToString()) { Dock = DockStyle.Fill }; form_Manual三角测试D.Show();
            Station_三角测量 station_三角D = new Station_三角测量(StationMgr.GetInstance().GetStation(Station.三角测试4.ToString()), new Param_CamTriTest(), new Point_CamTriTest(), new Config_CamTriTest());
            StationMgr.GetInstance().AddStation(new StationForm(station_三角D, station_三角D.station_Param, station_三角D.station_Point, station_三角D.station_Config, form_Manual三角测试D), Station.三角测试4.ToString(), station_三角D);

            Form_ManualTable form_ManualTable = new Form_ManualTable(Station.转盘.ToString()) { Dock = DockStyle.Fill }; form_ManualTable.Show();
            Station_Table station_Table = new Station_Table(StationMgr.GetInstance().GetStation(Station.转盘.ToString()), new Param_Table(), new Point_Table(), new Config_Table());
            StationMgr.GetInstance().AddStation(new StationForm(station_Table, station_Table.station_Param, station_Table.station_Point, station_Table.station_Config, form_ManualTable), Station.转盘.ToString(), station_Table);


            Station_Load station_Load = new Station_Load(StationMgr.GetInstance().GetStation(Station.上料.ToString()), new Param_Load(), new Point_Load(), new Config_Load());
            StationMgr.GetInstance().AddStation(new StationForm(station_Load, station_Load.station_Param, station_Load.station_Point, station_Load.station_Config, null), Station.上料.ToString(), station_Load);
        }
        public static bool Funtion_运行前处理()
        {
            //List<string> list_Err = HY_AllConsumables.ConsumeFactory.Ins._Consume.GetWarningList();
            //if (list_Err != null && list_Err.Count > 0)
            //{
            //    string msg = "";
            //    foreach (var item in list_Err)
            //    {
            //        msg += $"{item},";
            //    }
            //    if (AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.耗材使用完毕, new string[] { "继续运行", "退出运行" }, new string[] { $"耗材使用完毕:{msg}" }) == WaranResult.Custom2)
            //        return false;
            //}

            // TODO 需要识别一下治具是否正确
            StationStateHelper.OnTest_ShowStep("系统", "正在自动运行", LogType.Info);
            return true;
        }
        public static void Funtion_暂停时处理()
        {
            StationStateHelper.OnTest_ShowStep("系统", "正在暂停中", LogType.Warn);
        }
        public static void Funtion_恢复时处理()
        {
            StationStateHelper.OnTest_ShowStep("系统", "正在自动运行", LogType.Info);
        }
        public static void Funtion_停止时处理()
        {
            UserConfig.ResetIO();
            UPHTimeMgr.Instance.StopAll();
            LoadSeverMgr.Instance.Stop();
            GlobalVariable.g_StationState = StationState.StationStateEmg;
            StationStateHelper.OnTest_ShowStep("系统", "点击停止", LogType.Err);
        }
        public static bool Function_全机回零()
        {
            bool bResult = true;
            return true;
            try
            {
                int p = 0;
                // 先回平行光管 和鱼眼xiang
                Task task0 = Task.Run(() => {
                    bResult &= StationHelper.Station_Table.Function_复位平行光管();
                    p += 10;
                    Form1.OnTest_ShowHome("平行光管回原完成", p);
                });
                Task task1 = Task.Run(() => {
                    bResult &= StationHelper.Station_三角1.Function_平行光管鱼眼箱回零();
                    p += 30;
                    Form1.OnTest_ShowHome("平行光管鱼眼箱回原完成", p);
                });

                Task.WaitAll(new Task[] { task0, task1 });

                Task task2 = Task.Run(()=>
                    {
                        bResult &= StationHelper.Station_三角1.Function_工站回零();
                        p += 10;
                        Form1.OnTest_ShowHome("测量工位1回原完成", p);
                    });

                Task task3 = Task.Run(() =>
                {
                    bResult &= StationHelper.Station_三角2.Function_工站回零();
                    p += 10;
                    Form1.OnTest_ShowHome("测量工位2回原完成", p);
                });

                Task task4 = Task.Run(() =>
                {
                    bResult &= StationHelper.Station_三角3.Function_工站回零();
                    p += 10;
                    Form1.OnTest_ShowHome("测量工位3回原", p);
                });

                Task task5 = Task.Run(() =>
                {
                    bResult &= StationHelper.Station_三角4.Function_工站回零();
                    p += 10;
                    Form1.OnTest_ShowHome("测量工位4回原完成", p);
                });

                Task.WaitAll(new Task[] { task2, task3, task4, task5 });

                StationHelper.Station_Table.Function_工站回零();
                p += 10;
                Form1.OnTest_ShowHome("转盘回原完成", p);
                StationHelper.Station_Table.Function_Set平行光管参数();
                p += 10;
                Form1.OnTest_ShowHome("转盘回原完成", p);
                Form1.OnTest_ShowHome("回原完成", 100);
                return true;
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message);
                return false;
            }
        }
        public static void InitOtherParamter()
        {
            //VisionBaseMgr.GetInstance().Init(VisionType.HY视觉);
            //VisionBaseMgr.GetInstance().Function_Hide();
            //Thread.Sleep(500);
            //VisionBaseMgr.GetInstance().Function_切换机种("aoni");
            ////VisionDataMgr.GetInstance().Read();
            //Vision_组装定位Mgr.GetInstance().Read();
            //TrayDataMgr.GetInstance().Read();
            if (false)
            {
                //启用数据库
                DeviceDataCollect.GetInstance().Function_SaveInDataBase(true);
                DeviceDataCollect.GetInstance().DeleteHistoryFile(30);//30天
                SQLiteMgr.GetInstance().Init();
            }
            else
            {
                //不启用数据库
                DeviceDataCollect.GetInstance().Function_SaveInDataBase(false);
                DeviceDataCollect.GetInstance().DeleteHistoryFile(30);//30天

            }
            UPHTimeMgr.Instance.InitData();
            UPHTimeMgr.Instance.SetDirPath(@"D:\HY System", "AONI02");
            //下段用于更换保存内容后删除原本的数据库
            //  DBCustomTable.Function_DropDataBase();
            //DBCustomTable.Function_DropTable("TestResult锁付");
        }

    }
}
